﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.Expression.Interactivity.Core;
///
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Threading;
using GalaSoft.MvvmLight.Messaging;
using ScheduleMVVM.Model.PersistentFramework;
using ScheduleMVVM.Model.RuntimeFramework;
using ScheduleMVVM.Model.Miscellaneous;
using ScheduleMVVM.Localization;

namespace ScheduleMVVM.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        #region Binded properties
        public String ApplicationTitle
        {
            get { return GeneralResource.ApplicationTitle.ToLower(); }
        }
        public String WelcomeTitle
        {
            get { return ScheduleMVVM.Localization.GeneralResource.WelcomeString.ToLower(); }
        }
        public String FavouritesTitle
        {
            get { return GeneralResource.FavouritesPageTitle.ToLower(); }
        }
        public String Search
        {
            get { return GeneralResource.SearchPageTitle.ToLower(); }
        }
        public String RecentTitle
        {
            get { return GeneralResource.RecentTitle.ToLower(); }
        }
        public String EditMenuItemString
        {
            get { return GeneralResource.EditCommandString; }
        }
        public String DeleteMenuItemString
        {
            get { return GeneralResource.DeleteCommandString; }
        }
        public String PinMenuItemString
        {
            get { return GeneralResource.PinCommandString; }
        }
        public String SearchQueryString
        {
            get { return GeneralResource.SearchQueryString; }
        }
        public String AboutAppBarMenuItemString
        {
            get { return GeneralResource.AboutPivotItemTitle; }
        }
        public String RateAppBarMenuItemString
        {
            get { return GeneralResource.RateThisAppString; }
        }
        public RelayCommand MarketplaceReviewCommand
        {
            get
            {
                return new RelayCommand(delegate
                    {
                        MarketplaceReviewTask reviewTask = new MarketplaceReviewTask();
                        reviewTask.Show();
                    });
            }
        }
        public RelayCommand AboutCommand
        {
            get
            {
                return new RelayCommand(delegate
                {
                    var frame = App.Current.RootVisual as PhoneApplicationFrame;
                    frame.Navigate(new Uri("/YourLastAboutDialog;component/AboutPage.xaml", UriKind.Relative));
                });
            }
        }
        public RelayCommand SearchCommand
        {
            get
            {
                return new RelayCommand(delegate
                {
                    var frame = App.Current.RootVisual as PhoneApplicationFrame;
                    frame.Navigate(new Uri("/SearchPage.xaml", UriKind.Relative));
                });
            }
        }
        public RelayCommand OnLoadCommand
        {
            get
            {
                return new RelayCommand(ViewModelLocator.PostInitialization);
            }
        }
        public Int32 SelectedIndex
        { get; set; }
        private ObservableCollection<RouteEntryWrapper> _collection;
        public ObservableCollection<RouteEntryWrapper> Favourites
        {
            get
            {
                if (_collection == null)
                {
                    _collection = new ObservableCollection<RouteEntryWrapper>();
                    foreach (var i in AdvancedRuntimeContext.Current.Favourites) _collection.Add(new RouteEntryWrapper(i));
                }
                return _collection;
            }
        }
        public ObservableCollection<RouteEntryWrapper> Recent
        {
            get // TODO: Implement increment mechanism of refresh
            {
                ObservableCollection<RouteEntryWrapper> collection = new ObservableCollection<RouteEntryWrapper>();
                foreach (var i in AdvancedRuntimeContext.Current.Recent) collection.Add(new RouteEntryWrapper(i));
                return collection;
            }
        }
        #endregion
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                MessengerInstance = GalaSoft.MvvmLight.Messaging.Messenger.Default;
                MessengerInstance.Register<ListChangedMessage>(this, DispatchMessageListChanged);
            }
        }
        #region Message handlers
        private void DispatchMessageListChanged(ListChangedMessage message)
        {
            _collection = null;
            RaisePropertyChanged("Favourites");
            RaisePropertyChanged("Recent");
        }
        #endregion
        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }

}